﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class NewsListControl : YuanlinUserControl,
        IReplyConverter<NewsReply, GetListOperation>,
        IReplyConverter<NewsCategoryReply, GetListOperation>
    {
        protected ValuedDropDownList categoryDropDownList;
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        private EntityList newss;
        private int greenNum = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.GreenNum);
        private YuanlinUser loginUser = HttpContext.Current.User as YuanlinUser;
        private EntityList newsCategorys;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                NewsCategoryListRequest categoryRequest = new NewsCategoryListRequest();
                categoryRequest.UpNewsCategoryId = 0;
                categoryRequest.PageIndex = pager.PageIndex;
                categoryRequest.PageSize = pager.PageSize;
                requestBinders.Add(RequestHelper.BuildGetNewsCategoryListRequest(this, categoryRequest));

                NewsListRequest request = new NewsListRequest();
                request.PageIndex = pager.PageIndex;
                request.PageSize = pager.PageSize;
                request.OrderFields.Add(new OrderField(new News(), "IsTop", false));
                requestBinders.Add(RequestHelper.BuildGetNewsListRequest(this, request));
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            AjaxManager.Register(this, "AjaxMethod");
            categoryDropDownList.SelectedIndexChanged += new EventHandler(categoryDropDownList_SelectedIndexChanged);
        }
        protected void categoryDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            NewsListRequest request = new NewsListRequest();
            request.NewsCategoryId = Globals.ChangeType<int>(categoryDropDownList.SelectedValue);
            request.PageIndex = pager.PageIndex;
            request.PageSize = pager.PageSize;
            request.OrderFields.Add(new OrderField(new News(), "IsTop", false));
            newss = BusinessManager.GetNewsList(RequestHelper.BuildGetNewsListRequest(request));

            BindData(true);
        }
        private void BindData(bool isReBindData)
        {
            if (categoryDropDownList != null && !isReBindData)
            {
                NewsCategory newsCategory = new NewsCategory();
                newsCategory.Title.Value = "<全部>";
                newsCategory.EntityId.Value = 0;
                newsCategorys.Insert(0, newsCategory);

                categoryDropDownList.DataSource = newsCategorys;
                categoryDropDownList.DataTextField = "Title";
                categoryDropDownList.DataValueField = "EntityId";
                categoryDropDownList.DataBind();
            }
            if (list == null)
            {
                return;
            }

            list.DataSource = newss;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(newss.TotalCount);
                currentPage.TotalRecords = (newss.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }
        public string GetCategory(int newsCategoryId, int childNewsCategoryId)
        {
            string categoryTitle = "";
            NewsCategory newsCategory = BusinessManager.GetNewsCategory(newsCategoryId);
            if (newsCategory != null)
            {
                categoryTitle = newsCategory.Title + "/";
            }
            newsCategory = BusinessManager.GetNewsCategory(childNewsCategoryId);
            if (newsCategory != null)
            {
                categoryTitle += newsCategory.Title;
            }
            return categoryTitle;
        }

        #region Ajax Method

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public void DeleteNews(int newsId)
        {
            News news = BusinessManager.GetNews(newsId);

            Area area = BusinessManager.GetArea(news.Province.Value);
            area.NewsCount.Value--;
            BusinessManager.UpdateArea(area);

            area = BusinessManager.GetArea(news.City.Value);
            area.NewsCount.Value--;
            BusinessManager.UpdateArea(area);

            area = BusinessManager.GetArea(news.Town.Value);
            area.NewsCount.Value--;
            BusinessManager.UpdateArea(area);

            area = BusinessManager.GetArea(news.SmallTown.Value);
            area.NewsCount.Value--;
            BusinessManager.UpdateArea(area);

            BusinessManager.DeleteNews(newsId);
            YuanlinUser user = UserManager.GetUser(greenNum) as YuanlinUser;

            NewsListRequest request = new NewsListRequest();
            request.CompanyTypes = user.CompanyTypes.Value;
            request.PageIndex = 0;
            request.PageSize = 1;
            EntityList newlist = BusinessManager.GetNewsList(RequestHelper.BuildGetNewsListRequest(request));
            if (newlist.Count == 0)
            {
                user.Business = "";
               UserManager.UpdateUser(user);
            }
        }
        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public int DeleteNewss(string entityIds)
        {
            if (string.IsNullOrEmpty(entityIds))
            {
                return 0;
            }
            string[] entityIdArray = entityIds.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int entityId = 0;
            foreach (string stringEntityId in entityIdArray)
            {
                try
                {
                    entityId = Int32.Parse(stringEntityId);
                }
                catch
                {
                    continue;
                }
                if (entityId > 0)
                {
                    DeleteNews(entityId);                
                }
            }
            return 0;
        }

        #endregion

        #region IReplyConverter<NewsReply,GetListOperation> Members

        public void ConvertReply(NewsReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                newss = reply.EntityList;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    BindData(false);
                }
            }
        }

        #endregion

        #region IReplyConverter<NewsCategoryReply,GetListOperation> Members

        public void ConvertReply(NewsCategoryReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                newsCategorys = reply.EntityList;
            }
        }

        #endregion
    }
}